<template>
  <div>
    <el-form :inline="true" v-show="isSearchCollapse" class="query-form" ref="searchForm" :model="searchForm"
             @keyup.enter.native="refreshList()" @submit.native.prevent>
      <!--          搜索框-->
      <!--		     <el-form-item prop="cpu">
                 <el-input size="small" v-model="searchForm.cpu" placeholder="cpu信息" clearable></el-input>
               </el-form-item>
              <el-form-item prop="memory">
                <el-input size="small" v-model="searchForm.memory" placeholder="内存信息" clearable></el-input>
              </el-form-item>
              <el-form-item prop="disk">
                <el-input size="small" v-model="searchForm.disk" placeholder="硬盘信息" clearable></el-input>
              </el-form-item>
              <el-form-item prop="androidVersion">
                <el-input size="small" v-model="searchForm.androidVersion" placeholder="安卓版本号" clearable></el-input>
              </el-form-item>
              <el-form-item prop="macAddress">
                <el-input size="small" v-model="searchForm.macAddress" placeholder="mac地址" clearable></el-input>
              </el-form-item>
              <el-form-item prop="equipmentNumber">
                <el-input size="small" v-model="searchForm.equipmentNumber" placeholder="设备号" clearable></el-input>
              </el-form-item>
              <el-form-item prop="gps">
                <el-input size="small" v-model="searchForm.gps" placeholder="gps信息" clearable></el-input>
              </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="refreshList()" size="small">查询</el-button>
                  <el-button @click="resetSearch()" size="small">重置</el-button>
                </el-form-item>-->
      <el-form-item prop="storeNo">
        <el-input size="small" v-model="searchForm.storeNo" placeholder="店号" clearable></el-input>
      </el-form-item>
      <el-form-item prop="storeName">
        <el-input size="small" v-model="searchForm.storeName" placeholder="店名" clearable></el-input>
      </el-form-item>
      <el-form-item prop="ip">
        <el-input size="small" v-model="searchForm.ip" placeholder="ip地址" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="refreshList()" size="small">查询</el-button>
        <el-button @click="resetSearch()" size="small">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 导入导出-->
    <!--<el-form :inline="true" v-show="isImportCollapse"  class="query-form" ref="importForm">
       <el-form-item>
        <el-button type="default" @click="downloadTpl()" size="small">下载模板</el-button>
       </el-form-item>
       <el-form-item prop="loginName">
          <el-upload
            class="upload-demo"
            :action="`${this.$http.BASE_URL}/test/tobacco/tobaccoForm/import`"
            :on-success="uploadSuccess"
             :show-file-list="true">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只允许导入“xls”或“xlsx”格式文件！</div>
          </el-upload>
      </el-form-item>
    </el-form>-->
    <el-row>
<!--      <el-button v-if="hasPermission('test:tobacco:tobaccoForm:add')" type="primary" size="small" icon="el-icon-plus"
                 @click="add()">新建
      </el-button>
      <el-button v-if="hasPermission('test:tobacco:tobaccoForm:edit')" type="warning" size="small"
                 icon="el-icon-edit-outline" @click="edit()"
                 :disabled="dataListSelections.length != 1" plain>修改
      </el-button>
      <el-button v-if="hasPermission('test:one:leaveForm:del')" type="danger" size="small" icon="el-icon-delete"
                 @click="del()"
                 :disabled="dataListSelections.length <= 0" plain>删除
      </el-button>-->
      <el-button-group class="pull-right">
        <el-button
          type="default"
          size="small"
          icon="el-icon-search"
          @click="isSearchCollapse = !isSearchCollapse, isImportCollapse=false">
        </el-button>
        <!--            <el-button v-if="hasPermission('test:one:leaveForm:import')" type="default" size="small" icon="el-icon-upload2" title="导入" @click="isImportCollapse = !isImportCollapse, isSearchCollapse=false"></el-button>
                    <el-button v-if="hasPermission('test:one:leaveForm:export')" type="default" size="small" icon="el-icon-download" title="导出" @click="exportExcel()"></el-button>-->
        <el-button
          type="default"
          size="small"
          icon="el-icon-refresh"
          @click="refreshList">
        </el-button>
      </el-button-group>
    </el-row>
    <el-table
      :data="dataList"
      border
      size="medium"
      @selection-change="selectionChangeHandle"
      @sort-change="sortChangeHandle"
      :cell-style="cellStyle"
      v-loading="loading"
      class="table">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="storeNo"
        show-overflow-tooltip
        sortable="custom"
        label="店号">
      </el-table-column>
      <el-table-column
        prop="storeName"
        show-overflow-tooltip
        sortable="custom"
        label="店名">
      </el-table-column>
      <el-table-column
        prop="ip"
        show-overflow-tooltip
        sortable="custom"
        label="ip地址">
      </el-table-column>
<!--      <el-table-column
        prop="cpuDate"
        show-overflow-tooltip
        sortable="custom"
        label="cpu年份">
      </el-table-column>-->
      <el-table-column
        prop="osName"
        show-overflow
        sortable="custom"
        label="系统名称">
      </el-table-column>
      <el-table-column
        prop="osArch"
        show-overflow-tooltip
        sortable="custom"
        label="系统架构">
      </el-table-column>
      <el-table-column
        prop="cpu"
        sortable="custom"
        label="处理器">
      </el-table-column>
      <el-table-column
        prop="memorySize"
        show-overflow-tooltip
        sortable="custom"
        label="内存">
      </el-table-column>
      <el-table-column
        prop="diskSize"
        show-overflow-tooltip
        sortable="custom"
        label="磁盘空间">
      </el-table-column>
      <el-table-column
        prop="mac"
        label="mac地址"
        sortable="custom">
      </el-table-column>
      <el-table-column
        prop="baseBoard.manufacturer"
        show-overflow-tooltip
        label="主板制造商">
      </el-table-column>
      <!--      <el-table-column
              prop="gps"
              show-overflow-tooltip
              sortable="custom"
              label="gps地址">
            </el-table-column>
            <el-table-column
              header-align="center"
              align="center"
              fixed="right"
              width="200"
              label="操作">
              <template  slot-scope="scope">
                <el-button v-if="hasPermission('test:tobacco:tobaccoForm:view')" type="text" icon="el-icon-view" size="small" @click="view(scope.row.id)">查看</el-button>
                <el-button v-if="hasPermission('test:tobacco:tobaccoForm:edit')" type="text" icon="el-icon-edit" size="small" @click="edit(scope.row.id)">修改</el-button>
                <el-button v-if="hasPermission('test:tobacco:tobaccoForm:del')" type="text"  icon="el-icon-delete" size="small" @click="del(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>-->
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNo"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      background
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <!--    <LeaveFormForm  ref="leaveFormForm" @refreshDataList="refreshList"></LeaveFormForm>-->
  </div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import UserSelect from '@/components/userSelect'
  import CityPicker from '@/components/cityPicker'

  export default {
    data() {
      return {
        searchForm: {
          office: {
            id: ''
          },
          tuser: {
            id: ''
          },
          area: '',
          beginDate: '',
          ip: '',
          storeNo: '',
          storeName: '',
          endDate: '',
          remarks: ''
        },
        dataList: [],
        spanArr: [],
        position: 0,
        pageNo: 1,
        pageSize: 10,
        total: 0,
        orderBy: '',
        dataListSelections: [],
        isSearchCollapse: true,
        isImportCollapse: false,
        loading: false
      }
    },
    components: {
      SelectTree,
      UserSelect,
      CityPicker
    },
    activated() {
      this.refreshList()
    },
    methods: {
      // 获取数据列表
      refreshList() {
        this.loading = true
        this.$http({
          url: '/test/hardware/list',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            'orderBy': this.orderBy,
            beginBeginDate: this.searchForm.beginDate[0],
            endBeginDate: this.searchForm.beginDate[1],
            ...this.lodash.omit(this.searchForm, 'beginDate')
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.dataList = data.page.list
            this.total = data.page.count
            this.loading = false
            for (let i = 0; i < this.dataList.length; i++) {
            let mac = this.dataList[i].mac;
            let reg=new RegExp(';','g')//g代表全部
            let rel=new RegExp('"','g')//g代表全部
              let replace = mac.replace(rel,'');
              this.dataList[i].mac=replace.replace(reg,';\n')
            // this.dataList[i].mac = JSON.stringify(mac).replace(reg, ' &#10;')
            }
          }
        })
      },
      cellStyle ({ row, column, rowIndex, columnIndex }) {
        // 状态列字体颜色
        //系统名称
        if (columnIndex === 5) {
          if (row.updateStatus.indexOf('1') >= '0') {
            return 'color: red'
          }
        }
        //系统架构
        if (columnIndex === 6) {
          if (row.updateStatus.indexOf('2') >= '0') {
            return 'color: red'
          }
        }
        //处理器
        if (columnIndex === 7) {
          if (row.updateStatus.indexOf('3') >= '0') {
            return 'color: red'
          }
        }
        //内存
        if (columnIndex === 8) {
          if (row.updateStatus.indexOf('4') >= '0') {
            return 'color: red'
          }
        }
        //磁盘空间
        if (columnIndex === 9) {
          if (row.updateStatus.indexOf('5') >= '0') {
            return 'color: red'
          }
        }
        //mac地址
        if (columnIndex === 10) {
          if (row.updateStatus.indexOf('6') >= '0') {
            return 'color: red'
          }
        }
        //主板制造商
        if (columnIndex === 11) {
          if (row.updateStatus.indexOf('7') >= '0') {
            return 'color: red'
          }
        }
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageNo = val
        this.refreshList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 排序
      sortChangeHandle(obj) {
        if (obj.order === 'ascending') {
          this.orderBy = obj.prop + ' asc'
        } else if (obj.order === 'descending') {
          this.orderBy = obj.prop + ' desc'
        } else {
          this.orderBy = ''
        }
        this.refreshList()
      },
      // 新增
      add() {
        this.$refs.leaveFormForm.init('add', '')
      },
      // 修改
      edit(id) {
        id = id || this.dataListSelections.map(item => {
          return item.id
        })[0]
        this.$refs.leaveFormForm.init('edit', id)
      },
      // 查看
      view(id) {
        this.$refs.leaveFormForm.init('view', id)
      },
      // 删除
      del(id) {
        let ids = id || this.dataListSelections.map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/test/tobacco/tobaccoForm/delete',
            method: 'delete',
            params: {'ids': ids}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      },
      // 导入成功
      uploadSuccess(res, file) {
        if (res.success) {
          this.$message.success({
            dangerouslyUseHTMLString: true,
            message: res.msg
          })
        } else {
          this.$message.error(res.msg)
        }
      },
      // 下载模板
      downloadTpl() {
        this.$utils.download('/test/tobacco/tobaccoForm/import/template')
      },
      exportExcel() {
        this.$utils.download('/test/tobacco/tobaccoForm/export')
      },
      resetSearch() {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      }
    }
  }
</script>
<style>
  .el-table .cell {
    white-space: pre-line;
  }
</style>
